package com.onLineCar.management.template;

import com.onLineCar.common.core.domain.Result;


import com.onLineCar.common.core.utils.StringUtils;

import com.onLineCar.management.domain.PayRequest;
import com.onLineCar.management.domain.Reconciliation;
import com.onLineCar.management.domain.RequestOrder;
import com.onLineCar.management.domain.request.OrderRequest;
import com.onLineCar.management.domain.request.RequestOrders;
import com.onLineCar.management.feign.OrderData;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : 段敏强
 * @className : OrderDataFeign
 * @description : 描述说明该类的功能
 * @createTime : 2023/12/18 19:31
 */

/**
 * 订单数据服务
 * @author  段敏强
 */
@Component
public class OrderDataFeign {


    @Autowired
    private OrderData orderData;



    public Result getPlaceAnOrder(OrderRequest orderRequest){
        RequestOrder tblOrder = new RequestOrder();
        tblOrder.setOrderNumber(String.valueOf(orderRequest.getOrderNumber()));
        tblOrder.setPassengerInfoId(Long.valueOf(orderRequest.getPassengerInfoId()));
        tblOrder.setPassengerPhone(orderRequest.getPassengerPhone());
        tblOrder.setStartLongitude(orderRequest.getStartLongitude());
        tblOrder.setStartLatitude(orderRequest.getStartLatitude());
        tblOrder.setStartAddress(orderRequest.getStartAddress());
        tblOrder.setEndAddress(orderRequest.getEndAddress());
        tblOrder.setStartTime(orderRequest.getStartTime());
        tblOrder.setTimeInCar(orderRequest.getTimeInCar());
        tblOrder.setEndLongitude(orderRequest.getPassengerGetoffLatitude());
        tblOrder.setEndLatitude(orderRequest.getPassengerGetoffLongitude());
        tblOrder.setVehicleLevel(orderRequest.getLevelId());
        tblOrder.setOrderMoney(orderRequest.getOrderMoney());
        tblOrder.setSumKm(orderRequest.getSumKm());
        tblOrder.setCityCode(orderRequest.getCityCode());
        Result<?> result = orderData.placeAnOrder(tblOrder);
        if (Result.FAIL == result.getCode()){
            throw new RuntimeException("你有正在进行的订单或有未支付的订单");
        }
        return Result.success(result.getData());
    }


    public RequestOrder getOrderByOrderId(String orderNumber){
        RequestOrder tblOrder = new RequestOrder();
        tblOrder.setOrderNumber(orderNumber);
        Result<RequestOrder> result = orderData.selectOrderNumber(tblOrder);
        if (Result.FAIL == result.getCode()){
            throw new RuntimeException("获取订单失败");
        }
        return result.getData();
    }

    /**
     * 根据用户id获取订单
     * @param userId
     * @return
     */
    public RequestOrder getSelectByUserId(String userId){
        Result<RequestOrder> tblOrderResult = orderData.carryOut(Integer.valueOf(userId));
        if (Result.FAIL == tblOrderResult.getCode()){
            throw new RuntimeException("获取订单失败");
        }
            return tblOrderResult.getData();
    }


    /**
     * 获取订单号
     * @return
     */
    public String getOrderNumber(){
        String anOrderNumber = orderData.createAnOrderNumber();
        if (StringUtils.isBlank(anOrderNumber)){
            throw new RuntimeException("获取订单号失败");
        }
        return anOrderNumber;
    }

    /**
     * 更新用户到达
     */
    public void getUpdateUserReach(String orderNumber){
        Result result = orderData.updateUserReach(orderNumber);
        if (Result.FAIL == result.getCode()){
             throw new RuntimeException("网络出差了!!!!!");
        }
        Result.success();
    }

    public void getCancelOrder(RequestOrders tblOrder){
        Reconciliation reconciliation = new Reconciliation();
        reconciliation.setOrderNumber(tblOrder.getOrderNumber());
        reconciliation.setPassengerInfoId(tblOrder.getPassengerInfoId());
        reconciliation.setPassengerPhone(tblOrder.getPassengerPhone());
        Result result = orderData.cancelOrder(reconciliation);
        if (Result.FAIL == result.getCode()){
            throw new RuntimeException("取消订单失败");
        }
    }


}
